"""
公钥加密私钥解密
私钥加密公钥解密
"""
from Crypto.PublicKey import RSA
import base64
from Crypto.Cipher import PKCS1_v1_5

if __name__ == "__main__":
    # 生成密钥
    # rsaKey = RSA.generate(1024)

    # # 公钥提取
    # with open('rsa.public','wb')as f:
    #     f.write(rsaKey.public_key().export_key())
    # # 私钥提取
    # with open('rsa.private','wb')as f:
    #     f.write(rsaKey.export_key())
    
    #加密
    # test = "我喜欢你"
    # with open('rsa.private','r')as f:
    #     pk = f.read()
    #     rsa_pk = RSA.import_key(pk)
    #     rsa = PKCS1_v1_5.new(rsa_pk)
    #     result = rsa.encrypt(test.encode())
    #     print(base64.b64encode(result).decode())


    # 解密
    data = "dkBGa2RaliT4J7911L4sEf04oozgHXGCnxNmUn9k3pwBaFc5ibCgTfd0hu5rCSGbToOrLSk/HHraDDIlqUCqy4Eg4N8w0heRP2J70+N+EEpzIfhGnYDvmd+h283MoaQ0wGfGUb5wWowj9D0eolBVV69I7i4kuvTV06JM2R0Yb9c="
    with open('rsa.public','r')as f:
        pk = f.read()
        rsa_pk = RSA.import_key(pk)
        rsa = PKCS1_v1_5.new(rsa_pk)
        # base64解码
        initCode = base64.b64decode(data)
        print(initCode)
        resu = rsa.decrypt(initCode)
        print(resu)